package geez.widget;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.*;

public class Geez {

    private static long componentName = 1;

    public static GButton button(String html) {
        return new GButton(html);
    }

    public static GButton button(String name, String html, ClickListener clickListener) {
        return new GButton(html, clickListener).id(name);
    }

    public static GCheckBox checkBox() {
        return new GCheckBox();
    }

    public static GCheckBox checkBox(String label, boolean checked) {
        return checkBox(label, label, checked);
    }

    public static GCheckBox checkBox(String label, String name, boolean checked) {
        return checkBox(label, name, false, checked);
    }

    public static GCheckBox checkBox(String label, String name, boolean asHtml, boolean checked) {
        GCheckBox checkBox = new GCheckBox(label, asHtml).id(name);
        checkBox.setChecked(checked);
        return checkBox;
    }

    public static GHTML html() {
        return new GHTML();
    }

    public static GHTML html(String html) {
        return new GHTML(html);
    }

    public static GSpan span(String html) {
        return new GSpan(html);
    }

    public static GHidden hidden() {
        return new GHidden();
    }

    public static GHidden hidden(String name, String value) {
        return new GHidden(name, value);
    }

    public static GImage image(String url) {
        return new GImage(url);
    }

    public static GLabel label() {
        return label("");
    }

    public static GLabel label(String label) {
        return label("label" + componentName++, label);
    }

    public static GLabel label(String id, String label) {
        return new GLabel(label).id(id);
    }

    public static GListBox list(String id, String[] items, String selectedItem, int visibleItems, boolean multiple) {
        GListBox listBox = new GListBox(multiple);
        listBox.setVisibleItemCount(visibleItems);
        return listBox.addItems(items, selectedItem).id(id);
    }


    public static GListBox combo(String name, String[] items, String defaultItem) {
        return list(name, items, defaultItem, 1, false);
    }

    public static GTextBox numericBox(String name) {
        GTextBox numericBox = textBox(name).text("0.0");
        numericBox.addKeyboardListener(new KeyboardListenerAdapter() {
            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if ((!Character.isDigit(keyCode)) && (keyCode != (char) KEY_TAB)
                        && (keyCode != (char) KEY_BACKSPACE)
                        && (keyCode != (char) KEY_DELETE) && (keyCode != (char) KEY_ENTER)
                        && (keyCode != (char) KEY_HOME) && (keyCode != (char) KEY_END)
                        && (keyCode != (char) KEY_LEFT) && (keyCode != (char) KEY_UP)
                        && (keyCode != (char) KEY_RIGHT) && (keyCode != (char) KEY_DOWN)) {
                    // TextBox.cancelKey() suppresses the current keyboard event.
                    ((TextBox) sender).cancelKey();
                }
            }
        });
        return numericBox;
    }

    public static GTextBox textBox(String name) {
        return new GTextBox().id(name);
    }

    public static GTextBox textBox(String name, String style) {
        return textBox(name).css(style);
    }

    public static GFlowPanel flowPanel() {
        return new GFlowPanel();
    }

    public static GVerticalPanel verticalPanel() {
        return new GVerticalPanel();
    }

    public static <T extends UIObject> T withNameAndId(T widget, String name) {
        DOM.setElementAttribute(widget.getElement(), "name", name);
        DOM.setElementAttribute(widget.getElement(), "id", name);
        return widget;
    }

    public static <T extends UIObject> T withStyle(T widget, String style, String[] optionalClasses) {
        widget.setStylePrimaryName(style);
        for (String optionalClass : optionalClasses) {
            widget.addStyleName(optionalClass);
        }
        return widget;
    }

    public static GLabel clearOnly() {
        return label().style("clear", "both");
    }
}
